package huaweiod.third;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 连续出牌数量
 * https://fcqian.blog.csdn.net/article/details/127711078
 */
public class C_CardsPlayed {

    public static int ans = 0;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] nums = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        String[] colors = in.nextLine().split(" ");
        boolean[] visit = new boolean[nums.length];
        for (int i = 0; i < nums.length; i++) {
            visit[i] = true;
            dfs(1, nums[i], colors[i], visit, nums, colors);
            visit[i] = false;
        }
        System.out.println(ans);
    }

    private static void dfs(int count, int preNum, String preColor, boolean[] visit, int[] nums, String[] colors) {
        ans = Math.max(ans, count);
        for (int i = 0; i < nums.length; i++) {
            if (!visit[i] && (nums[i] == preNum || colors[i].equals(preColor))) {
                visit[i] = true;
                dfs(count + 1, nums[i], colors[i], visit, nums, colors);
                visit[i] = false;
            }
        }
    }
}
